package com.tomato.zip.service;

import com.tomato.zip.enums.ZipType;
import com.tomato.zip.errors.ZipErrorType;
import com.tomato.zip.exception.ZipException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 7Z相关压缩包
 */
@Component
@Slf4j
public class SevenZService implements BaseZip {
    @Override
    public ZipType getZipType() {
        return ZipType.SEVEN_Z;
    }

    @Override
    public void compress(File zip, String password, List<File> files) {
        try {
            if (zip == null ) {
                throw new ZipException(ZipErrorType.COMPRESS_FILE_NULL);
            }
            fileExists(files);
        } catch (Exception e) {
            log.error("压缩文件异常:{}", e);
            throw new ZipException(e.getMessage());
        }
    }

    @Override
    public void extractAll(File zip, String password, String path) {

    }

    @Override
    public void extractFile(File zip, String fileName, String password, String path) {

    }

    @Override
    public void addFile(File zip, String password, File file) {

    }

    private SevenZFile createSevenZFile(File zip, String password) throws IOException {
        if (isLegal(zip)) {
            throw new ZipException(ZipErrorType.COMPRESS_FILE_NOT_SUPPORT);
        }
        return StringUtils.isBlank(password) ? new SevenZFile(zip) : new SevenZFile(zip, password.toCharArray());
    }
}
